﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.VisualStudio.Modeling.Diagrams;
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Shell.Interop;
using MsVsShell = Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio;

namespace devtm.AutoMapper
{
    /// <summary>
    /// Interaction logic for LstBox.xaml
    /// </summary>
    public partial class LstBox : UserControl
    {
        public LstBox()
        {
            InitializeComponent();
        }

        private void SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (e.AddedItems != null)
            {
                foreach (var item in e.AddedItems)
                {

                    /*
                    BoxItemMap b = item as BoxItemMap;
                    if (b != null)
                    {


                        MapperShape mm = PresentationViewsSubject.GetPresentation(b.Object.Mapper).FirstOrDefault() as MapperShape;
                        Microsoft.VisualStudio.Modeling.Diagrams.Diagram diagram = mm.Diagram;
                        DiagramView diagramClientView = diagram.ActiveDiagramView;

                        //using (Transaction trans = mm.Store.TransactionManager.BeginTransaction("reorder the diagram"))
                        //{
                        //    ElementListCompartment ml = new ElementListCompartment(mm.Partition, new PropertyAssignment(b.Object.Id, b.Object));
                        //    ShapeElement sh = mm.NestedChildShapes[0];
                        //    var aa = new DiagramItem(sh);
                        //    if (sh.CanFocus)
                        //        diagramClientView.Selection.FocusedItem = aa;
                        //    diagramClientView.Selection.Set(diagramClientView.Selection.FocusedItem);
                        //}
                        //IShapeIterator l = new toto();
                        //sh.IterateShapes(l);

                        
                        //ElementListCompartment
                        var ll = diagramClientView.Selection.FocusedItem;

                        if (ll != null)
                        {
                            var oo = ll.Shape;
                        }


                    }

                    */

                }
            }
            

            if (SelectionChangedEventHandler != null)
                SelectionChangedEventHandler(sender, e);

        }

        private class toto : IShapeIterator
        {

            public void OnShape(ShapeElement shape)
            {
                throw new NotImplementedException();
            }
        }

        private static void SelectShape(NodeShape mm)
        {

            try
            {

                Microsoft.VisualStudio.Modeling.Diagrams.Diagram diagram = mm.Diagram;

                if (diagram != null)
                {

                    DiagramView diagramClientView = diagram.ActiveDiagramView;

                    if ((diagramClientView != null))
                    {

                        var aa = new DiagramItem(mm);

                        if (mm.CanFocus)
                            diagramClientView.Selection.FocusedItem = aa;

                        diagramClientView.DiagramClientView.EnsureVisible(mm.AbsoluteBoundingBox, mm.EnsureVisiblePreference);
                        diagramClientView.Selection.Set(diagramClientView.Selection.FocusedItem);

                    }

                }

            }
            catch (Exception)
            { }


        }

        public event EventHandler<SelectionChangedEventArgs> SelectionChangedEventHandler;
       
    }
}
